
/**
 * 加载数据
 */
function loadData() {
	plus.nativeUI.showWaiting("loading",{
		width:"100px",
		height:"100px"
	});
	var date=new Date();
	mui.ajax(DIS_DATA, {
		data:{
			type:mMode,
			hour:mHour
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒
		success: function(data) {
			if(data.code == 200) {
				var dArray = data.obj.data;
				map.centerAndZoom(new BMap.Point(centerLng, centerLat), zoom);
				loadPoint(dArray);
				plus.nativeUI.closeWaiting();
			}else{
				plus.nativeUI.closeWaiting();
				mui.alert(data.msg);
			}
		},
		error: function(e) {
			plus.nativeUI.closeWaiting();
			mui.alert("服务器连接超时，请稍后再试。")
		}
	});
}


var dataMap={};
/**
 * 渲染新点
 */
function loadPoint(dArray){
	map.clearOverlays();
	for(var i=0;i<dArray.length;i++){
		var dataArr=dArray[i];
		addMarker(dArray[i]);
	}
}

/**
 * 添加点
 * @param {Object} pointData
 */
function addMarker(pointData) {
	var point = new BMap.Point(pointData.lng, pointData.lat);
	var myCompOverlay = new ComplexCustomOverlay(point, pointData);
	map.addOverlay(myCompOverlay);
}

/**
 * 切换查看模式
 * @param {Object} mode
 */
function changeMode(mode) {
	mMode = mode;
	centerLng=map.getCenter().lng;
	centerLat=map.getCenter().lat;
	zoom=map.getZoom();
	loadData();
}

function ComplexCustomOverlay(point, pointData) {
	this._point = point;
	this._pointData = pointData;
	this._warn_level=0;
	this._warnArr=[
		{
			src:'images/warn/l1.png',
			back:'#3CB371',
			text:'无预警'
		},
		{
			src:'images/warn/l2.png',
			back:'#F4EA2A',
			text:'黄色预警'
		},
		{
			src:'images/warn/l3.png',
			back:'#E07206',
			text:'橙色预警'
		},
		{
			src:'images/warn/l4.png',
			back:'#D81E06',
			text:'红色预警'
		}
	];
}
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map) {
	this._map = map;
	var div = this._div = document.createElement("div");
	div.style.position = "absolute";
	div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
	div.style.color = "white";
	div.style.minWidth="20px";
	div.style.textAlign="center";
	div.style.backgroundColor="#6EBB84";
	div.style.height = "20px";
	div.style.padding = "2px";
	div.style.lineHeight = "18px";
	div.style.MozUserSelect = "none";
	div.style.border = "none";
	div.style.borderRadius="5px";
	div.style.fontSize = "14px"
	
	var valueBox = this._span = document.createElement("div");
	div.appendChild(valueBox);
	
	var value = this._span = document.createElement("div");
	value.style.float="left";
	value.style.textAlign="center";
	valueBox.appendChild(value);
	
	var important = this._span = document.createElement("div");
	important.style.width="15px";
	important.style.height="15px";
	important.style.marginLeft="3px";
	important.style.float="left";
	
	var arrow = this._arrow = document.createElement("div");
	arrow.style.position = "absolute";
	arrow.style.width = "12px";
	arrow.style.height = "10px";
	arrow.style.top = "20px";
	arrow.style.left = "2px";
	arrow.style.overflow = "hidden";
	div.appendChild(arrow);
	
	var pData=this._pointData;
	value.innerText=pData.t_val;
	
	for(var i=0;i<DIS_AREA[mMode-1].length;i++){
		if(pData.t_val>DIS_AREA[mMode-1][i].min && pData.t_val<=DIS_AREA[mMode-1][i].max){
			this._warn_level=i+1;
			break;
		}
	}
	var warnObj=this._warnArr[this._warn_level];
	arrow.style.background = "url(../../images/warn/l"+(this._warn_level+1)+".png) no-repeat";
	div.style.backgroundColor = warnObj.back;
	
	var divSize=value.innerText.length*8+4; 
	if(mMode==1 && pData.d_ct!=null){
		divSize+=20;
		important.style.background="url(../../images/warn/important"+pData.d_ct+".png)";
		important.style.backgroundSize="100% 100%";
		important.style.backgroundRepeat="no-repeat";
		valueBox.appendChild(important);
	}else if(mMode==2 && pData.d_ct!=null){
		for(var i=0;i<DIS_AREA[mMode-1].length;i++){
			if(pData.d_ct>DIS_AREA[mMode-1][i].min && pData.d_ct<=DIS_AREA[mMode-1][i].max){
				divSize+=20;
				important.style.background="url(../../images/warn/important"+(i+1)+".png)";
				important.style.backgroundSize="100% 100%";
				important.style.backgroundRepeat="no-repeat";
				valueBox.appendChild(important);
				break;
			}
		}
	}else if(mMode==3 && pData.d_ct!=null){
		for(var i=0;i<DIS_AREA[mMode-1].length;i++){
			if(pData.d_ct>DIS_AREA[mMode-1][i].min && pData.d_ct<=DIS_AREA[mMode-1][i].max){
				divSize+=20;
				important.style.background="url(../../images/warn/important"+(i+1)+".png)";
				important.style.backgroundSize="100% 100%";
				important.style.backgroundRepeat="no-repeat";
				valueBox.appendChild(important);
				break;
			}
		}
	}
	div.style.width=divSize+"px";
	
	//由于onclick事件只能在pc响应，手机上响应ontouchstart
	div.ontouchstart = function() {
		mui.openWindow({
			url: 'index_sub.html',
			id: 'damageWarn/index_sub.html',
			extras: {
				lng:pData.lng,
				lat:pData.lat,
				mMode:mMode
			},
			show: {
				anishow: 'pop-in'
			},
			styles: {
				popGesture: 'close'
			},
			waiting: {
				autoshow: true
			}
		});
	}
	map.getPanes().labelPane.appendChild(div);

	return div;
}
ComplexCustomOverlay.prototype.draw = function() {
	var map = this._map;
	var pixel = map.pointToOverlayPixel(this._point);
	this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
	this._div.style.top = pixel.y - 30 + "px";
}

//选择模式控件
function ZoomControl() {
	// 默认停靠位置和偏移量
	this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
	this.defaultOffset = new BMap.Size(20, 20); //边距
}
ZoomControl.prototype = new BMap.Control();
ZoomControl.prototype.initialize = function(map) {
	// 创建一个DOM元素
	var select = document.createElement("select");
	// 添加文字说明
	for(var i = 0; i < modeArr.length; i++) {
		var opt = document.createElement("option");
		opt.innerText = modeArr[i];
		opt.setAttribute("value", i + 1);
		if(i+1==mMode){
			opt.selected='selected';
		}
		select.appendChild(opt);
	}
	// 设置样式
	select.style.width = "90px";
	select.style.background="#5FB878";
	select.style.color="white";
	// 绑定事件
	select.onchange = function(e) {
		changeMode(this.value);
	}
	// 添加DOM元素到地图中
	map.getContainer().appendChild(select);
	// 将DOM元素返回
	return select;
}

//切换小时控件
function ChangeHourControl(lr,x,y,text,actived) {
	// 默认停靠位置和偏移量
	if(lr=='right'){
		this.defaultAnchor = BMAP_ANCHOR_BOTTOM_RIGHT;
	}else{
		this.defaultAnchor = BMAP_ANCHOR_BOTTOM_LEFT;
	}
	this.defaultOffset = new BMap.Size(x, y); //边距
	this._btnText=text;
	this._actived=actived;
}
ChangeHourControl.prototype = new BMap.Control();
ChangeHourControl.prototype.initialize = function(map) {
	// 创建一个DOM元素
	var btn=document.createElement('button');
	btn.style.background='#009688';
	btn.style.color="white";
	btn.innerText=this._btnText;
	// 绑定事件
	btn.onclick = this._actived
	// 添加DOM元素到地图中
	map.getContainer().appendChild(btn);
	// 将DOM元素返回
	return btn;
}